class MainSence extends egret.DisplayObjectContainer {

    constructor() {
        super();
        this.init();
    }

    public babyRockArr:Array<any> = [];
    public bg:egret.Bitmap;
    public bg_hill:egret.Bitmap;
    private BG_HILL_OFFSETY = 473;

    private init() {
        this.createBabyRocks();
        this.createRobot3();
        this.createBackground();
        this.createHill();
        MainSenceManager.mainSenceEarthQuake = this.earthQuakeStart.bind(this);
    }

    public createBackground() {
        var bgTexture:egret.Texture = RES.getRes('bg4_jpg');
        this.bg = new egret.Bitmap(bgTexture);
        this.addChildAt(this.bg, 0);
    }

    public createHill() {
        var bgTexture:egret.Texture = RES.getRes('bg_hill_png');
        this.bg_hill = new egret.Bitmap(bgTexture);
        this.addChildAt(this.bg_hill, 1);
        this.bg_hill.y = this.BG_HILL_OFFSETY;
    }

    public createRobot3() {
        var robot3 = new Robot3();
        robot3.x = 550;
        robot3.y = 570;
        robot3.scaleX = 0.3;
        robot3.scaleY = 0.3;
        this.addChildAt(robot3, 100);

        this.createCtrls(robot3);
    }

    public createCtrls(robot3) {
        var btnGroup = new ButtonGroup(robot3);
        btnGroup.x = 80;
        btnGroup.y = 650;
        this.addChild(btnGroup);
    }

    /**** 机器人踩地板 start ****/
    private EARTH_QUAKE_TIMER:egret.Timer = null;
    private EARTH_QUAKE_OFFSET:number = 4;

    private initEarthQuakeTimer() {
        let interval = 34; //震动间隔毫秒数
        let num = 2; //震动次数
        this.EARTH_QUAKE_TIMER = new egret.Timer(interval, num);
        this.EARTH_QUAKE_TIMER.addEventListener(egret.TimerEvent.TIMER, this.earthQuake, this);
        this.EARTH_QUAKE_TIMER.addEventListener(egret.TimerEvent.TIMER_COMPLETE, ()=>{
            this.EARTH_QUAKE_TIMER.reset();
        }, this);
    };

    private earthQuake() {
        if(this.bg_hill.y === this.BG_HILL_OFFSETY) {
            //图像未震动
            this.bg_hill.y += this.EARTH_QUAKE_OFFSET;
            this.babyRockArr.map((br, i)=>{
                br.jump()
            })
        }else {
            //图像震动复位
            this.bg_hill.y = this.BG_HILL_OFFSETY;
        }
        
    }

    public earthQuakeStart() {
        if(this.EARTH_QUAKE_TIMER === null) {
            this.initEarthQuakeTimer();
        }
        this.EARTH_QUAKE_TIMER.start();
    }
    /**** 机器人踩地板 end ****/

    public createBabyRocks() {
        var br = new BabyRock();
        br.name = 'br1';
        br.x = 560;
        br.y = 660;
        br.scaleX = -1;
        this.addChild(br);
        this.babyRockArr.push(br);

        br = new BabyRock();
        br.name = 'br2';
        br.x = 600;
        br.y = 665;
        br.scaleX = 0.8;
        br.scaleY = 0.8;
        this.addChild(br);
        this.babyRockArr.push(br);

        br = new BabyRock();
        br.name = 'br3';
        br.x = 110;
        br.y = 540;
        br.scaleX = -0.7;
        br.scaleY = 0.7;
        this.addChild(br);
        this.babyRockArr.push(br);

        br = new BabyRock();
        br.name = 'br4';
        br.x = 240;
        br.y = 455;
        br.scaleX = -0.7;
        br.scaleY = 0.7;
        this.addChild(br);
        this.babyRockArr.push(br);
    }
}